<?php
namespace App\Api\Movie;

use PhalApi\Api;
use App\Domain\Movie\Advertisement as AdvertisementDomain;
use PhalApi\UCloud\UCloud_Lite;
/**
 * 广告接口
 */
class Advertisement extends Api
{
    public function getRules()
    {
        return array(
            'getList' => array(
            ),
            'searchList' => array(
                'domain'=>array('name'=>'domain','require'=>false),
            ),
            'update' => array(
                'id' => array('name' => 'id', 'require' => false),
                'ad_pic'=>array('name'=> 'ad_pic','require'=>true),
                'ad_desc'=>array('name'=> 'ad_desc', 'require'=>true),
                'domain'=>array('name'=>'domain', 'require'=>true)
            ),
            'upload' => array(
                'file' => array(
                    'name' => 'file',        // 客户端上传的文件字段
                    'type' => 'file',
                    'require' => true,
                    'max' => 2097152,        // 最大允许上传2M = 2 * 1024 * 1024,
                    'range' => array('image/jpeg', 'image/png'),  // 允许的文件格式
                    'ext' => 'jpeg,jpg,png', // 允许的文件扩展名
                    'desc' => '待上传的图片文件',
                ),
            ),
            'delete'=> array(
                'id'=> array('name'=>'id','require'=>true)
            ),
            'getInfo'=> array(
                'id'=> array('name'=>'id','require'=>true)
            )
        );
    }
    /**
     * 获取广告列表
     * @desc 用于后端获取广告列表
     */
    public function getList() {
        $domain = new AdvertisementDomain();
        $res = $domain->getList();
        return $res;
    }
    /**
     * 搜索广告列表
     * @desc 用于后端搜索广告列表
     */
    public function searchList() {
        $domain = new AdvertisementDomain();
        $res = $domain->searchList($this->domain);
        return $res;
    }
    /**
     * 更新或添加广告
     * @desc 有id则是更新无id则是添加
     */
    public function update() {
        $domain = new AdvertisementDomain();
        $data=array(
            'ad_pic'=>$this->ad_pic,
            'ad_desc'=>$this->ad_desc,
            'domain'=>$this->domain,
            'update_time'=>date('Y-m-d H:i:s',time())
        );
        $res = $domain->update($this->id,$data);
        return $res;
    }
    /**
     * 删除广告
     * @desc 删除广告
     */
    public function delete() {
        $domain = new AdvertisementDomain();
        $res = $domain->delete($this->id);
        return array($res);
    }
    /**
     * 获取广告
     * @desc 获取广告信息
     */
    public function getInfo() {
        $domain = new AdvertisementDomain();
        $res = $domain->get($this->id);
        return $res;
    }
    /**
     * 上传文件
     * @desc 用于后端上传文件使用
     */
    public function upload() {
        $rs = array('code' => 0, 'url' => '');

        $tmpName = $this->file['tmp_name'];

        $name = md5($this->file['name'] . $_SERVER['REQUEST_TIME']);
        $ext = strrchr($this->file['name'], '.');
        $uploadFolder = sprintf('%s/public/uploads/', API_ROOT);
        if (!is_dir($uploadFolder)) {
            mkdir($uploadFolder, 0777);
        }

        $imgPath = $uploadFolder .  $name . $ext;
        if (move_uploaded_file($tmpName, $imgPath)) {
            $rs['code'] = 1;
            $rs['url'] = sprintf('http://%s/uploads/%s%s', $_SERVER['SERVER_NAME'], $name, $ext);
        }

        return $rs;
    }

}
